package treegenerator;

import com.mxgraph.examples.swing.GraphEditor.CustomGraph;
import com.mxgraph.examples.swing.GraphEditor.CustomGraphComponent;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import com.mxgraph.examples.swing.editor.BasicGraphEditor;
import com.mxgraph.examples.swing.editor.EditorActions;
import com.mxgraph.examples.swing.editor.EditorActions.StylesheetAction;
import com.mxgraph.examples.swing.editor.EditorPalette;
import com.mxgraph.examples.swing.editor.SchemaEditorMenuBar;
import com.mxgraph.examples.swing.editor.SchemaEditorToolBar;
import com.mxgraph.examples.swing.editor.SchemaGraphComponent;
import com.mxgraph.io.mxCodec;
import com.mxgraph.io.vdx.mxStyleSheet;
import com.mxgraph.io.vdx.mxStyleSheetManager;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.util.mxResources;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxCellState;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.text.Document;

public class SchemaEditor extends BasicGraphEditor {

    static mxGraph graph;
    static Object parent;
    private static final long serialVersionUID = -7007225006753337933L;


    public SchemaEditor(GenericTreeNode<String> n) {
        this(n, "TreeGenerator", new CustomGraphComponent(new CustomGraph()));
    }

    public SchemaEditor(GenericTreeNode<String> n, String appTitle, mxGraphComponent component) {
        super(appTitle, component);

        graph = getGraphComponent().getGraph();
        parent = graph.getDefaultParent();
        graph.getModel().beginUpdate();
        try {
            mxCell v1 = (mxCell) graph.insertVertex(parent, null, n.getData(), xValue, yValue, anchoNodo, altoNodo);
            imprimirHijosRecursivo(n, xValue, yValue, v1);
        } finally {
            graph.getModel().endUpdate();
        }
    }

    protected void installToolBar() {
        add(new SchemaEditorToolBar(this, JToolBar.HORIZONTAL),
                BorderLayout.NORTH);
    }

    public static void imprimirHijos(GenericTreeNode<String> n) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        SchemaEditor editor = new SchemaEditor(n);
        javax.swing.Action a = editor.graphLayout("verticalTree", true);
        a.actionPerformed(null);
        cambiarEstilo("/com/mxgraph/examples/swing/resources/basic-style.xml");
        SchemaEditorMenuBar menuBar = new SchemaEditorMenuBar(editor);
        editor.createFrame(menuBar).setVisible(true);
    }

    static void cambiarEstilo(String stylesheet) {
        mxCodec codec = new mxCodec();
        org.w3c.dom.Document doc = mxUtils.loadDocument(EditorActions.class.getResource(stylesheet).toString());
        if (doc != null) {
            codec.decode(doc.getDocumentElement(),
                    graph.getStylesheet());
            graph.refresh();
        }
    }
    static int xValue = 100;
    static int yValue = 100;
    static int nodeEspacing = 10;
    static int altoNodo = 20;
    static int anchoNodo = 20;

    static void imprimirHijosRecursivo(GenericTreeNode<String> n, int x, int y,mxCell v1) {
        if (n.hasChildren()) {
            List<GenericTreeNode<String>> hijos = n.getChildren();
            y += (yValue / 2);
            for (int j = 0; j < n.getNumberOfChildren(); j++) {
                mxCell v2 = (mxCell) graph.insertVertex(parent, null, hijos.get(j).getData(),
                        x, y, anchoNodo, altoNodo);
                graph.insertEdge(parent, null, "", v1, v2, "Straight");
                imprimirHijosRecursivo(hijos.get(j), x, y, v2);
            }
        }
    }
}
